from django.db import transaction
from models import UserProfile
from forms import UserProfileForm
from django.shortcuts import get_object_or_404, render_to_response
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django.views.generic.list_detail import object_list, object_detail
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from django.core import serializers
from django.contrib.auth.decorators import login_required, permission_required

def user_profile(request,user_name):
    pass

def user_profile_detail(request, object_id):
    return object_detail(
        request,
        queryset = UserProfile.objects.all(),
        object_id = object_id,
        template_object_name = 'user_profile'
    )
user_profile_detail = login_required(user_profile_detail)

def user_profile_home(request):
    return user_profile_detail(request, UserProfile.objects.get_profile(request.user).pk)
user_profile_home = login_required(user_profile_home)

def user_profile_update_ajax(request, object_id):
    user_profile = get_object_or_404(UserProfile,pk=object_id)
    form = UserProfileForm(Request.POST or None)
    response={}
    if form.is_valid():
        data = f.cleaned_data
        for field in data:
            setattr(user_profile,field,data[field])
        user_profile.save()
        response.update({"success":"true","user_profile":user_profile.values})
        return HttpResponse(simplejson.dumps(response), mimetype='application/javascript')
    errors = {}
    for field in form.fields:
        errors.update({field:f[field].errors})
    response.update({"success":"false","errors":errors})
    return HttpResponse(simplejson.dumps(response), mimetype='application/javascript')
user_profile_update_ajax = login_required(user_profile_update_ajax)